<template>
    <div class="login-wrap">
        <div class="ms-title">挂号系统注册</div>
        <div class="ms-login">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
                <el-form-item prop="username">
                    <el-input v-model="ruleForm.username" placeholder="请填写用户名"></el-input>
                </el-form-item>
                <el-form-item prop="email">
                    <el-input v-model="ruleForm.email" placeholder="请填写邮箱地址"></el-input>
                </el-form-item>  
                <el-form-item prop="password">
                    <el-input type="password" placeholder="请填写密码" v-model="ruleForm.password" @keyup.enter.native="submitForm('ruleForm')"></el-input>
                </el-form-item>
                <el-form-item prop="repassword">
                    <el-input type="password" placeholder="请输入密码" v-model="ruleForm.repassword" @keyup.enter.native="submitForm('ruleForm')"></el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
                </div>
                <p style="font-size:12px;line-height:30px;color:#999;text-align: right;"><router-link :to="{path:'./login'}">返回登录</router-link></p>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
    	data(){
		      var checkmail = (rule, value, callback) => {
		      	var reg=/^\+?[a-z0-9](([-+.]|[_]+)?[a-z0-9]+)*@([a-z0-9]+(\.|\-))+[a-z]{2,6}$/;
		      	if(!reg.test(value)){
		      		callback(new Error('邮箱格式填写错误'));
		      	}else{
		      		callback();
		      	}
		      };
		      var checkpassword = (rule, value, callback) => {
		      	var passreg_step1=/^[A-Z][A-Z0-9a-z]{7,}$/;
		      	if(!passreg_step1.test(value)){
		      		callback(new Error('密码首字符必须大写，密码长度不小于8位'));
		      	}else{
		      		callback();
		      	}
		      };
		      var checkrepassword = (rule, value, callback) => {
		      	if(value != this.ruleForm.password){
		      		callback(new Error('两次输入的密码不一致'));
		      	}else if(value == ''){
		      		callback(new Error('请先输入密码'));
		      	}else{
		      		callback();
		      	}
		      };
    		return{
                ruleForm: {
                    email: '',
                    username:'',
                    password: '',
                    repassword:''
                },
                rules: {
                    username: [
                        { required: true, message: '用户名必填', trigger: 'blur' }
                    ],
                    email: [
                        { validator: checkmail,trigger: 'blur' }
                    ],
                    password: [
                        { validator: checkpassword,trigger: 'blur' }
                    ],
                    repassword:[
                        { validator: checkrepassword,trigger: 'blur' }        	
                    ]
                }
    		}
    	},
        created:function(){

        },
        methods: {
            submitForm(formName) {
            	var data=this.ruleForm;
                const self = this;
                self.$refs[formName].validate((valid) => {
                    if (valid) {
                        //ajax
					        self.$http.post("http://www.yuyue.com/api/register",
					          {
					              name:self.ruleForm.username,email:self.ruleForm.email,password:self.ruleForm.password
					          },
					          {emulateJSON:true}
					        ).then(response=>{
					        	var data=response.data;
					        	if(data.code== 200){
					        		this.throw_notice('注册成功');
					        		self.$router.push('./login');
					        	}else{
					        		this.throw_notice(data.msg);
					        	}
					        },error=>{
					        	if(error.status == 422){
					        		if(error.data.email != undefined ){
					        			this.throw_notice(error.data.email[0]);
					        		}else if(error.data.name != undefined){
					        			this.throw_notice(error.data.name[0]);
					        		}
					        	}else{
					        		this.throw_notice('注册失败1');
					        	}
					        })
                    } else {
                    	this.throw_notice('注册失败');
                        this.$store.dispatch('print_notice');
                        return false;
                    }
                });
            }
        }
    }
</script>

<style scoped>
    .login-wrap{
        position: relative;
        width:100%;
        height:100%;
    }
    .ms-title{
        position: absolute;
        top:50%;
        width:100%;
        margin-top: -260px;
        text-align: center;
        font-size:30px;
        color: #fff;

    }
    .ms-login{
        position: absolute;
        left:50%;
        top:50%;
        width:300px;
        margin:-200px 0 0 -190px;
        padding:40px 40px 10px 40px;
        border-radius: 5px;
        background: #fff;
    }
    .login-btn{
        text-align: center;
    }
    .login-btn button{
        width:100%;
        height:36px;
    }

</style>